package com.pic.mgt.syushin.action.shell;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;

import com.pic.mgt.syushin.action.SyushinPackageScheduleMgtActionImpl;
import com.pic.mgt.telnetd.AbstractCommandPerformer;

public class SyushinPackageScheduleMgtCommandPerformer extends
		AbstractCommandPerformer {
	
	private final String commandName = "bkmgt";
	private final String commandDescritpion = "manage syushin batch business package schedule.";
	
	private SyushinPackageScheduleMgtActionImpl syushinPackageScheduleMgtActionImpl;

	@Override
	protected void addOption(Options options) {
		options.addOption("add", false, "add business package schedule. (req: bn,sn,ex ; opt: ds)");
		options.addOption("rmv", false, "remove business package schedule. (req: bn,sn)");
		options.addOption("mod", false, "modify business package schedule time or schedule description. (req: bn,sn ; opt: ex,ds)");
		options.addOption("pas", false, "pause business package schedule. (req: bn or al)");
		options.addOption("res", false, "resume business package schedule. (req: bn or al)");
		options.addOption("pac", false, "package batch business files immediately. (req: bn)");
		options.addOption("lis", false, "list the business package schedule. (req: bn)");
		options.addOption("bn", true, "assign business name.");
		options.addOption("sn", true, "assign schedule name.");
		options.addOption("ds", true, "assign schedule description. (notice: put description in the middle of double quote.)");
		options.addOption("ex", true, "assign cron expression time of schedule. ex: \"0 * 14 * * ?\" (notice: double quote is required.)");
		options.addOption("al", false, "means all business. (This parameter is only for action 'pas' or 'res'.");
	}

	@Override
	protected String doCommandPerform(CommandLine commandLine) {
		if (commandLine.hasOption("add")) {
			if (!commandLine.hasOption("bn") ||
				!commandLine.hasOption("sn") ||
				!commandLine.hasOption("ex")) {
				return
				super.getReplyMessageWithReplyCode(
						"did not provide business name, schedule name or cron schedule expression.",
						this.COMMAND_PERFORM_FAILED_CODE);
			}
			
			// -------------------------------------------
			
			boolean isAddScheduleSuccessful =
					this.syushinPackageScheduleMgtActionImpl.addSchedule(
									commandLine.getOptionValue("bn"),
									commandLine.getOptionValue("sn"),
									commandLine.getOptionValue("ds"),
									commandLine.getOptionValue("ex"));
			
			if (isAddScheduleSuccessful) {
				return super.getCommandPerformFinishMsg();
			}
			else {
				return super.getCommandPerformFailedMsg();
			}
		}
		else if(commandLine.hasOption("rmv")) {
			if (!commandLine.hasOption("bn") ||
					!commandLine.hasOption("sn")) {
					return
					super.getReplyMessageWithReplyCode(
							"did not provide business name or schedule name.",
							this.COMMAND_PERFORM_FAILED_CODE);
				}
			
			// -------------------------------------------
			
			boolean isRemoveScheduleSuccessful =
					this.syushinPackageScheduleMgtActionImpl.removeSchedule(
							commandLine.getOptionValue("bn"),
							commandLine.getOptionValue("sn"));
			
			if (isRemoveScheduleSuccessful) {
				return super.getCommandPerformFinishMsg();
			}
			else {
				return super.getCommandPerformFailedMsg();
			}
		}
		else if(commandLine.hasOption("mod")) {
			if (!commandLine.hasOption("bn") ||
				!commandLine.hasOption("sn")) {
				return
				super.getReplyMessageWithReplyCode(
						"did not provide business name or schedule name.",
						this.COMMAND_PERFORM_FAILED_CODE);
			}

			// -------------------------------------------

			boolean isModifyScheduleSuccessful =
				this.syushinPackageScheduleMgtActionImpl.modifySchedule(
						commandLine.getOptionValue("bn"),
						commandLine.getOptionValue("sn"),
						commandLine.getOptionValue("ds"),
						commandLine.getOptionValue("ex"));

			if (isModifyScheduleSuccessful) {
				return super.getCommandPerformFinishMsg();
			}
			else {
				return super.getCommandPerformFailedMsg();
			}
		}
		else if (commandLine.hasOption("lis")) {
			if (!commandLine.hasOption("bn")) {
					return
					super.getReplyMessageWithReplyCode(
							"did not provide business name.",
							this.COMMAND_PERFORM_FAILED_CODE);
				}
				
			// -------------------------------------------
			
			String packagingScheduleInfoString = this.syushinPackageScheduleMgtActionImpl.
							getPackagingScheduleInfoByBizName(commandLine.getOptionValue("bn"));
			
			if (null != packagingScheduleInfoString) {
				return getReplyMessageWithReplyCode(packagingScheduleInfoString,
						                            this.COMMAND_PERFORM_SUCCESS_CODE);
			}
			else {
				return super.getCommandPerformFailedMsg();
			}
		}
		else if (commandLine.hasOption("pac")) {
			if (!commandLine.hasOption("bn")) {
				return
				super.getReplyMessageWithReplyCode(
						"did not provide business name.",
						this.COMMAND_PERFORM_FAILED_CODE);
			}

			// -------------------------------------------

			boolean isPackagingBusinessSuccessful =
				this.syushinPackageScheduleMgtActionImpl.packagingBusiness(commandLine.getOptionValue("bn"));

			if (isPackagingBusinessSuccessful) {
				return super.getCommandPerformFinishMsg();
			}
			else {
				return super.getCommandPerformFailedMsg();
			}
		}
		else if (commandLine.hasOption("pas")) {
			if (!commandLine.hasOption("bn") && !commandLine.hasOption("al")) {
				return
				super.getReplyMessageWithReplyCode(
						"did not provide business name.",
						this.COMMAND_PERFORM_FAILED_CODE);
			}
			
			if (commandLine.hasOption("bn") && commandLine.hasOption("al")) {
				return
				super.getReplyMessageWithReplyCode(
						"'bn' and 'al' are incompatible.",
						this.COMMAND_PERFORM_FAILED_CODE);
			}

			// -------------------------------------------
			
			boolean isPauseBusinessPackageScheduleSuccessful = false;
			
			if (commandLine.hasOption("bn")) {
				isPauseBusinessPackageScheduleSuccessful =
						this.syushinPackageScheduleMgtActionImpl.pausePackagingSchedule(commandLine.getOptionValue("bn"));
			}
			else if(commandLine.hasOption("al")) {
				isPauseBusinessPackageScheduleSuccessful =
					this.syushinPackageScheduleMgtActionImpl.pausePackagingSchedule();
			}
			
			if (isPauseBusinessPackageScheduleSuccessful) {
				return super.getCommandPerformFinishMsg();
			}
			else {
				return super.getCommandPerformFailedMsg();
			}
		}
		else if (commandLine.hasOption("res")) {
			if (!commandLine.hasOption("bn") && !commandLine.hasOption("al")) {
				return
				super.getReplyMessageWithReplyCode(
						"did not provide business name.",
						this.COMMAND_PERFORM_FAILED_CODE);
			}
			
			if (commandLine.hasOption("bn") && commandLine.hasOption("al")) {
				return
				super.getReplyMessageWithReplyCode(
						"'bn' and 'al' are incompatible.",
						this.COMMAND_PERFORM_FAILED_CODE);
			}

			// -------------------------------------------
			
			boolean isResumeBusinessPackageScheduleSuccessful = false;
			
			if (commandLine.hasOption("bn")) {
				isResumeBusinessPackageScheduleSuccessful =
						this.syushinPackageScheduleMgtActionImpl.resumePackagingSchedule(commandLine.getOptionValue("bn"));
			}
			else if(commandLine.hasOption("al")) {
				isResumeBusinessPackageScheduleSuccessful =
					this.syushinPackageScheduleMgtActionImpl.resumePackagingSchedule();
			}
			
			if (isResumeBusinessPackageScheduleSuccessful) {
				return super.getCommandPerformFinishMsg();
			}
			else {
				return super.getCommandPerformFailedMsg();
			}
		}
		else {
			return
				super.getReplyMessageWithReplyCode(
						"did not specify management action (option: add, rmv, mod, pac, pas, res or lis).",
						this.COMMAND_PERFORM_FAILED_CODE);
		}
	}

	@Override
	public String getCommandDescription() {
		return this.commandDescritpion;
	}

	@Override
	public String getCommandName() {
		return this.commandName;
	}

	public void setSyushinPackageScheduleMgtActionImpl(
			SyushinPackageScheduleMgtActionImpl syushinPackageScheduleMgtActionImpl) {
		this.syushinPackageScheduleMgtActionImpl = syushinPackageScheduleMgtActionImpl;
	}

}
